
<!DOCTYPE HTML>
<html lang="zh-hans" >
    <head>
        <meta charset="UTF-8">
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <title>1.Vue.js 3.0中的响应式 · Gary的小窝</title>
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="description" content="">
        <meta name="generator" content="GitBook 3.2.3">
        <meta name="author" content="Gary">
        
        
    
    <link rel="stylesheet" href="../gitbook/style.css">

    
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-prism/prism-solarizedlight.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-search-pro/search.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-expandable-chapters/expandable-chapters.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-splitter/splitter.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-tbfed-pagefooter/footer.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-anchor-navigation-ex/style/plugin.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-fontsettings/website.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-theme-comscore/test.css">
                
            
        

    

    

        
    
    
    <meta name="HandheldFriendly" content="true"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../gitbook/images/apple-touch-icon-precomposed-152.png">
    <link rel="shortcut icon" href="../gitbook/images/favicon.ico" type="image/x-icon">

    
    <link rel="next" href="el.html" />
    
    
    <link rel="prev" href="./" />
    

    </head>
    <body>
        
<div class="book">
    <div class="book-summary">
        
            
<div id="book-search-input" role="search">
    <input type="text" placeholder="输入并搜索" />
</div>

            
                <nav role="navigation">
                


<ul class="summary">
    
    

    

    
        
        
    
        <li class="chapter " data-level="1.1" data-path="../">
            
                <a href="../">
            
                    
                    写在前面
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2" data-path="../leetcode/">
            
                <a href="../leetcode/">
            
                    
                    Leetcode之旅
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.2.1" data-path="../leetcode/1.html">
            
                <a href="../leetcode/1.html">
            
                    
                    1.两数之和
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.2" data-path="../leetcode/2.html">
            
                <a href="../leetcode/2.html">
            
                    
                    2.两数相加
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.3" data-path="../leetcode/3.html">
            
                <a href="../leetcode/3.html">
            
                    
                    3.无重复字符的最长子串
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.4" data-path="../leetcode/4.html">
            
                <a href="../leetcode/4.html">
            
                    
                    4.寻找两个有序数组的中位数
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.5" data-path="../leetcode/6.html">
            
                <a href="../leetcode/6.html">
            
                    
                    6.Z字形变换
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.6" data-path="../leetcode/9.html">
            
                <a href="../leetcode/9.html">
            
                    
                    9.回文数
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.7" data-path="../leetcode/11.html">
            
                <a href="../leetcode/11.html">
            
                    
                    11.盛最多水的容器
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.8" data-path="../leetcode/12.html">
            
                <a href="../leetcode/12.html">
            
                    
                    12.整数转罗马数字
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.9" data-path="../leetcode/13.html">
            
                <a href="../leetcode/13.html">
            
                    
                    13.罗马数字转整数
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.10" data-path="../leetcode/14.html">
            
                <a href="../leetcode/14.html">
            
                    
                    14.最长公共前缀
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.11" data-path="../leetcode/322.html">
            
                <a href="../leetcode/322.html">
            
                    
                    322.零钱兑换
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.12" data-path="../leetcode/72.html">
            
                <a href="../leetcode/72.html">
            
                    
                    72.编辑距离
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.3" data-path="../flutter/">
            
                <a href="../flutter/">
            
                    
                    Flutter从入门到放弃
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.3.1" data-path="../flutter/dart1.html">
            
                <a href="../flutter/dart1.html">
            
                    
                    1.Dart语言(1)
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.2" data-path="../flutter/dart2.html">
            
                <a href="../flutter/dart2.html">
            
                    
                    2.Dart语言(2)
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.3" data-path="../flutter/widget1.html">
            
                <a href="../flutter/widget1.html">
            
                    
                    3.Flutter初始化
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.4" data-path="../flutter/containerText.html">
            
                <a href="../flutter/containerText.html">
            
                    
                    4.Container和Text组件
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.5" data-path="../flutter/image.html">
            
                <a href="../flutter/image.html">
            
                    
                    5.Image组件
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.6" data-path="../flutter/chapter2.html">
            
                <a href="../flutter/chapter2.html">
            
                    
                    6.Flutter 技术总结
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.4" data-path="../vue/">
            
                <a href="../vue/">
            
                    
                    Vue组件精讲
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.4.1" data-path="../vue/1.html">
            
                <a href="../vue/1.html">
            
                    
                    1.provide/inject
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.2" data-path="../vue/2.html">
            
                <a href="../vue/2.html">
            
                    
                    2.手动实现broadcast和dispatch方法
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.3" data-path="form.html">
            
                <a href="form.html">
            
                    
                    3.自己动手实现带校验的Form表单组件
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.5" data-path="./">
            
                <a href="./">
            
                    
                    JavaScript进阶
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter active" data-level="1.5.1" data-path="proxy.html">
            
                <a href="proxy.html">
            
                    
                    1.Vue.js 3.0中的响应式
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.2" data-path="el.html">
            
                <a href="el.html">
            
                    
                    2.Element UI 源码初探
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.3" data-path="bind.html">
            
                <a href="bind.html">
            
                    
                    3.bind的模拟实现
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.4" data-path="proto.html">
            
                <a href="proto.html">
            
                    
                    4.JS原型和原型链
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.5" data-path="call.html">
            
                <a href="call.html">
            
                    
                    5.模拟实现call和apply
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.6" data-path="ui.html">
            
                <a href="ui.html">
            
                    
                    6.使用Vue cli3搭建组件库并发布到 npm
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.7" data-path="microFE.html">
            
                <a href="microFE.html">
            
                    
                    7.记一次基于qiankun的微前端的改造
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.8" data-path="git.html">
            
                <a href="git.html">
            
                    
                    8.Git的使用
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.6" data-path="../vueLibrary/">
            
                <a href="../vueLibrary/">
            
                    
                    基于Vue的PC端的组件库
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.6.1" data-path="../vueLibrary/1.html">
            
                <a href="../vueLibrary/1.html">
            
                    
                    1.环境搭建
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.7" data-path="../life/">
            
                <a href="../life/">
            
                    
                    网事杂谈
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.7.1" data-path="../life/1.html">
            
                <a href="../life/1.html">
            
                    
                    1.周杰伦专辑赏析--叶惠美
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7.2" data-path="../life/2.html">
            
                <a href="../life/2.html">
            
                    
                    2.周杰伦专辑赏析--七里香
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    

    

    <li class="divider"></li>

    <li>
        <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
            本书使用 GitBook 发布
        </a>
    </li>
</ul>


                </nav>
            
        
    </div>

    <div class="book-body">
        
            <div class="body-inner">
                
                    

<div class="book-header" role="navigation">
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href=".." >1.Vue.js 3.0中的响应式</a>
    </h1>
</div>




                    <div class="page-wrapper" tabindex="-1" role="main">
                        <div class="page-inner">
                            
<div id="book-search-results">
    <div class="search-noresults">
    
                                <section class="normal markdown-section">
                                
                                <div id="anchor-navigation-ex-navbar"><i class="fa fa-navicon"></i><ul><li><span class="title-icon "></span><a href="#vue20&#x548C;vuejs-30-&#x4E2D;&#x5B9E;&#x73B0;&#x54CD;&#x5E94;&#x5F0F;&#x7684;&#x65B9;&#x5F0F;&#x5206;&#x6790;"><b></b>Vue2.0&#x548C;Vue.js 3.0 &#x4E2D;&#x5B9E;&#x73B0;&#x54CD;&#x5E94;&#x5F0F;&#x7684;&#x65B9;&#x5F0F;&#x5206;&#x6790;</a></li><ul><li><span class="title-icon "></span><a href="#&#x524D;&#x8A00;"><b></b>&#x524D;&#x8A00;</a></li><li><span class="title-icon "></span><a href="#vue20&#x54CD;&#x5E94;&#x5F0F;&#x56DE;&#x987E;"><b></b>vue2.0&#x54CD;&#x5E94;&#x5F0F;&#x56DE;&#x987E;</a></li><li><span class="title-icon "></span><a href="#proxy&#x57FA;&#x7840;"><b></b>proxy&#x57FA;&#x7840;</a></li><li><span class="title-icon "></span><a href="#&#x5B9E;&#x73B0;&#x76EE;&#x6807;-"><b></b>&#x5B9E;&#x73B0;&#x76EE;&#x6807; </a></li><li><span class="title-icon "></span><a href="#&#x5B9E;&#x73B0;-reactive-&#x51FD;&#x6570;"><b></b>&#x5B9E;&#x73B0; reactive &#x51FD;&#x6570;</a></li><ul><li><span class="title-icon "></span><a href="#&#x9700;&#x8981;&#x4E00;&#x4E2A;&#x8F85;&#x52A9;&#x51FD;&#x6570;"><b></b>&#x9700;&#x8981;&#x4E00;&#x4E2A;&#x8F85;&#x52A9;&#x51FD;&#x6570;</a></li><li><span class="title-icon "></span><a href="#&#x4E3B;&#x4F53;&#x51FD;&#x6570;"><b></b>&#x4E3B;&#x4F53;&#x51FD;&#x6570;</a></li><li><span class="title-icon "></span><a href="#&#x5B8C;&#x6574;&#x4EE3;&#x7801;"><b></b>&#x5B8C;&#x6574;&#x4EE3;&#x7801;</a></li></ul></ul></ul></div><a href="#vue20&#x548C;vuejs-30-&#x4E2D;&#x5B9E;&#x73B0;&#x54CD;&#x5E94;&#x5F0F;&#x7684;&#x65B9;&#x5F0F;&#x5206;&#x6790;" id="anchorNavigationExGoTop"><i class="fa fa-arrow-up"></i></a><h1 id="vue20&#x548C;vuejs-30-&#x4E2D;&#x5B9E;&#x73B0;&#x54CD;&#x5E94;&#x5F0F;&#x7684;&#x65B9;&#x5F0F;&#x5206;&#x6790;"><a name="vue20&#x548C;vuejs-30-&#x4E2D;&#x5B9E;&#x73B0;&#x54CD;&#x5E94;&#x5F0F;&#x7684;&#x65B9;&#x5F0F;&#x5206;&#x6790;" class="anchor-navigation-ex-anchor" href="#vue20&#x548C;vuejs-30-&#x4E2D;&#x5B9E;&#x73B0;&#x54CD;&#x5E94;&#x5F0F;&#x7684;&#x65B9;&#x5F0F;&#x5206;&#x6790;"><i class="fa fa-link" aria-hidden="true"></i></a>Vue2.0&#x548C;Vue.js 3.0 &#x4E2D;&#x5B9E;&#x73B0;&#x54CD;&#x5E94;&#x5F0F;&#x7684;&#x65B9;&#x5F0F;&#x5206;&#x6790;</h1>
<h2 id="&#x524D;&#x8A00;"><a name="&#x524D;&#x8A00;" class="anchor-navigation-ex-anchor" href="#&#x524D;&#x8A00;"><i class="fa fa-link" aria-hidden="true"></i></a>&#x524D;&#x8A00;</h2>
<p>10&#x6708;5&#x53F7;&#x51CC;&#x6668;&#xFF0C;&#x5C24;&#x96E8;&#x6EAA;&#x516C;&#x5E03;&#x4E86; Vue 3 &#x7684;<a href="https://github.com/vuejs/vue-next" target="_blank">&#x6E90;&#x4EE3;&#x7801;</a>&#xFF0C;&#x76EE;&#x524D;&#x7684;&#x7248;&#x672C;&#x662F; Pre-Alpha &#x3002;
&#x6211;&#x4EEC;&#x77E5;&#x9053;Vue &#x7684;&#x6838;&#x5FC3;&#x4E4B;&#x4E00;&#x5C31;&#x662F;&#x54CD;&#x5E94;&#x5F0F;&#x7CFB;&#x7EDF;&#xFF0C;&#x901A;&#x8FC7;&#x4FA6;&#x6D4B;&#x6570;&#x636E;&#x7684;&#x53D8;&#x5316;&#xFF0C;&#x6765;&#x9A71;&#x52A8;&#x66F4;&#x65B0;&#x89C6;&#x56FE;&#x3002;&#x5728;Vue.js 2&#x7684;&#x7248;&#x672C;&#x4E2D;&#x662F;&#x901A;&#x8FC7; Object.defineProperty()&#x51FD;&#x6570;&#x6765;&#x5B9E;&#x73B0;&#x7684;&#x54CD;&#x5E94;&#x5F0F;&#x3002;&#x5927;&#x5BB6;&#x65E9;&#x5C31;&#x5F97;&#x77E5;&#x5728;Vue&#x65B0;&#x7248;&#x7684;&#x54CD;&#x5E94;&#x5F0F;&#x662F;&#x7528; Proxy &#x5B9E;&#x73B0;&#x7684;&#xFF0C;&#x73B0;&#x5728;&#x6211;&#x4EEC;&#x6765;&#x5229;&#x7528; Object.defineProperty()&#x548C; Proxy &#x5B9E;&#x73B0;&#x4E00;&#x4E2A;&#x57FA;&#x672C;&#x7684;&#x54CD;&#x5E94;&#x5F0F;&#x9AA8;&#x67B6;&#x3002;</p>
<h2 id="vue20&#x54CD;&#x5E94;&#x5F0F;&#x56DE;&#x987E;"><a name="vue20&#x54CD;&#x5E94;&#x5F0F;&#x56DE;&#x987E;" class="anchor-navigation-ex-anchor" href="#vue20&#x54CD;&#x5E94;&#x5F0F;&#x56DE;&#x987E;"><i class="fa fa-link" aria-hidden="true"></i></a>vue2.0&#x54CD;&#x5E94;&#x5F0F;&#x56DE;&#x987E;</h2>
<p>vue2.0&#x4E2D;&#x4F7F;&#x7528;Object.defineProperty()&#x51FD;&#x6570;&#x6765;&#x5B9E;&#x73B0;&#x7684;&#x54CD;&#x5E94;&#x5F0F;&#x3002;&#x91CD;&#x65B0;&#x5B9A;&#x4E49;&#x5C5E;&#x6027; &#x7ED9;&#x5C5E;&#x6027;&#x589E;&#x52A0;setter &#x548C;getter &#x65B9;&#x6CD5;</p>
<pre class="language-"><code class="lang-js"> <span class="token keyword">function</span> <span class="token function">updateView</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
     console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token string">&apos;update view&apos;</span><span class="token punctuation">)</span>
 <span class="token punctuation">}</span>
 <span class="token keyword">function</span> <span class="token function">observe</span><span class="token punctuation">(</span><span class="token parameter">obj</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
     <span class="token keyword">if</span><span class="token punctuation">(</span><span class="token keyword">typeof</span> obj <span class="token operator">!==</span> <span class="token string">&apos;object&apos;</span> <span class="token operator">||</span> data <span class="token operator">==</span> <span class="token keyword">null</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
         <span class="token keyword">return</span> obj
     <span class="token punctuation">}</span>
     <span class="token keyword">for</span><span class="token punctuation">(</span><span class="token keyword">let</span> key <span class="token keyword">in</span> obj<span class="token punctuation">)</span><span class="token punctuation">{</span>
         <span class="token function">defineObserve</span><span class="token punctuation">(</span>obj<span class="token punctuation">,</span>key<span class="token punctuation">,</span>data<span class="token punctuation">[</span>key<span class="token punctuation">]</span><span class="token punctuation">)</span>
     <span class="token punctuation">}</span>
 <span class="token punctuation">}</span>

 <span class="token keyword">function</span> <span class="token function">defineObserve</span><span class="token punctuation">(</span><span class="token parameter">obj<span class="token punctuation">,</span>key<span class="token punctuation">,</span>value</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
     Object<span class="token punctuation">.</span><span class="token function">defineProperty</span><span class="token punctuation">(</span>obj<span class="token punctuation">,</span>key<span class="token punctuation">,</span><span class="token punctuation">{</span>
         <span class="token keyword">set</span><span class="token punctuation">(</span>newVal<span class="token punctuation">)</span><span class="token punctuation">{</span>
             <span class="token keyword">if</span><span class="token punctuation">(</span>newVal <span class="token operator">!==</span> value<span class="token punctuation">)</span><span class="token punctuation">{</span>
                 <span class="token function">updateView</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
                 value <span class="token operator">=</span> newValue
             <span class="token punctuation">}</span>
         <span class="token punctuation">}</span><span class="token punctuation">,</span>
         <span class="token keyword">get</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
             <span class="token keyword">return</span> value
         <span class="token punctuation">}</span>
     <span class="token punctuation">}</span><span class="token punctuation">)</span>
 <span class="token punctuation">}</span>

 <span class="token comment">//example</span>
 <span class="token keyword">let</span> data <span class="token operator">=</span> <span class="token punctuation">{</span>name<span class="token punctuation">:</span><span class="token string">&apos;123&apos;</span><span class="token punctuation">}</span>
 <span class="token function">observe</span><span class="token punctuation">(</span>data<span class="token punctuation">)</span>
 data<span class="token punctuation">.</span>name <span class="token operator">=</span> <span class="token string">&apos;456&apos;</span>
</code></pre>
<p>&#x8FD9;&#x6837;&#x662F;&#x5B8C;&#x6574;&#x4E86;&#x521D;&#x6B65;&#x7684;&#x54CD;&#x5E94;&#x5F0F; &#x6539;&#x53D8;&#x6570;&#x636E;&#x7684;&#x65F6;&#x5019; &#x89E6;&#x53D1;&#x66F4;&#x65B0;&#x8BD5;&#x56FE;&#x7684;&#x65B9;&#x6CD5; &#x6570;&#x636E;&#x4E5F;&#x53D1;&#x751F;&#x4E86;&#x6539;&#x53D8;&#xFF0C;&#x4F46;&#x662F;&#x5982;&#x679C;&#x5BF9;&#x8C61;&#x4E2D;&#x6709;&#x5D4C;&#x5957;&#x7684;&#x5C5E;&#x6027;&#x5462;&#xFF0C;&#x4F8B;&#x5982;&#xFF1A;</p>
<pre class="language-"><code class="lang-js"><span class="token keyword">let</span> data <span class="token operator">=</span> <span class="token punctuation">{</span>name<span class="token punctuation">:</span><span class="token string">&apos;123&apos;</span><span class="token punctuation">,</span>age<span class="token punctuation">:</span><span class="token punctuation">{</span>n<span class="token punctuation">:</span><span class="token number">111</span><span class="token punctuation">}</span><span class="token punctuation">}</span>
 data<span class="token punctuation">.</span>age<span class="token punctuation">.</span>n <span class="token operator">=</span> <span class="token number">123456</span>
</code></pre>
<pre class="language-"><code class="lang-js"><span class="token keyword">let</span> data <span class="token operator">=</span> <span class="token punctuation">{</span>name<span class="token punctuation">:</span><span class="token string">&apos;123&apos;</span><span class="token punctuation">,</span>age<span class="token punctuation">:</span><span class="token punctuation">{</span>n<span class="token punctuation">:</span><span class="token number">111</span><span class="token punctuation">}</span><span class="token punctuation">}</span>
data<span class="token punctuation">.</span>age <span class="token operator">=</span><span class="token punctuation">{</span>n<span class="token punctuation">:</span><span class="token number">200</span><span class="token punctuation">}</span>
data<span class="token punctuation">.</span>age<span class="token punctuation">.</span>n <span class="token operator">=</span> <span class="token number">123456</span>
</code></pre>
<p>&#x8FD9;&#x4E24;&#x79CD;&#x60C5;&#x51B5;&#x4E0B; &#x4E0A;&#x8FF0;&#x4EE3;&#x7801;&#x5C31;&#x65E0;&#x6CD5;&#x6EE1;&#x8DB3;&#x8981;&#x6C42;&#x4E86; &#x5C31;&#x5F97;&#x4F7F;&#x7528;&#x9012;&#x5F52;&#x7684;&#x65B9;&#x6CD5; &#x53BB;&#x89C2;&#x5BDF;value</p>
<pre class="language-"><code class="lang-js"><span class="token keyword">function</span> <span class="token function">updateView</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
     console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token string">&apos;update view&apos;</span><span class="token punctuation">)</span>
 <span class="token punctuation">}</span>
 <span class="token keyword">function</span> <span class="token function">observe</span><span class="token punctuation">(</span><span class="token parameter">obj</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
     <span class="token keyword">if</span><span class="token punctuation">(</span><span class="token keyword">typeof</span> obj <span class="token operator">!==</span> <span class="token string">&apos;object&apos;</span> <span class="token operator">||</span> data <span class="token operator">==</span> <span class="token keyword">null</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
         <span class="token keyword">return</span> obj
     <span class="token punctuation">}</span>
     <span class="token keyword">for</span><span class="token punctuation">(</span><span class="token keyword">let</span> key <span class="token keyword">in</span> obj<span class="token punctuation">)</span><span class="token punctuation">{</span>
         <span class="token function">defineObserve</span><span class="token punctuation">(</span>obj<span class="token punctuation">,</span>key<span class="token punctuation">,</span>data<span class="token punctuation">[</span>key<span class="token punctuation">]</span><span class="token punctuation">)</span>
     <span class="token punctuation">}</span>
 <span class="token punctuation">}</span>

 <span class="token keyword">function</span> <span class="token function">defineObserve</span><span class="token punctuation">(</span><span class="token parameter">obj<span class="token punctuation">,</span>key<span class="token punctuation">,</span>value</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
     <span class="token comment">//&#x6DFB;&#x52A0;&#x9012;&#x5F52;</span>
     <span class="token function">observe</span><span class="token punctuation">(</span>value<span class="token punctuation">)</span>
     Object<span class="token punctuation">.</span><span class="token function">defineProperty</span><span class="token punctuation">(</span>obj<span class="token punctuation">,</span>key<span class="token punctuation">,</span><span class="token punctuation">{</span>
         <span class="token keyword">set</span><span class="token punctuation">(</span>newVal<span class="token punctuation">)</span><span class="token punctuation">{</span>
             <span class="token keyword">if</span><span class="token punctuation">(</span>newVal <span class="token operator">!==</span> value<span class="token punctuation">)</span><span class="token punctuation">{</span>
                 <span class="token comment">//&#x6DFB;&#x52A0;&#x9012;&#x5F52;</span>
                 <span class="token function">observe</span><span class="token punctuation">(</span>newVal<span class="token punctuation">)</span>
                 <span class="token function">updateView</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
                 value <span class="token operator">=</span> newValue
             <span class="token punctuation">}</span>
         <span class="token punctuation">}</span><span class="token punctuation">,</span>
         <span class="token keyword">get</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
             <span class="token keyword">return</span> value
         <span class="token punctuation">}</span>
     <span class="token punctuation">}</span><span class="token punctuation">)</span>
 <span class="token punctuation">}</span>

 <span class="token comment">//example</span>
<span class="token keyword">let</span> data <span class="token operator">=</span> <span class="token punctuation">{</span>name<span class="token punctuation">:</span><span class="token string">&apos;123&apos;</span><span class="token punctuation">,</span>age<span class="token punctuation">:</span><span class="token punctuation">{</span>n<span class="token punctuation">:</span><span class="token number">111</span><span class="token punctuation">}</span><span class="token punctuation">}</span>
 <span class="token function">observe</span><span class="token punctuation">(</span>data<span class="token punctuation">)</span>
 data<span class="token punctuation">.</span>age <span class="token operator">=</span><span class="token punctuation">{</span>n<span class="token punctuation">:</span><span class="token number">200</span><span class="token punctuation">}</span>
 data<span class="token punctuation">.</span>age<span class="token punctuation">.</span>n <span class="token operator">=</span> <span class="token number">123456</span>
</code></pre>
<p>&#x90A3;&#x4E48;&#x5BF9;&#x8C61;&#x5C42;&#x7EA7;&#x975E;&#x5E38;&#x7684;&#x6DF1;&#xFF0C;&#x9012;&#x5F52;&#x4F1A;&#x5BFC;&#x81F4;&#x6027;&#x80FD;&#x4E0D;&#x597D;&#x3002;
&#x95EE;&#x9898;2&#xFF1A;&#x5982;&#x679C;&#x5BF9;&#x8C61;&#x7684;&#x5C5E;&#x6027;&#x4E0D;&#x5B58;&#x5728; &#x65B0;&#x589E;&#x7684;&#x5C5E;&#x6027;&#x4E5F;&#x662F;&#x6CA1;&#x6709;&#x54CD;&#x5E94;&#x5F0F;&#x7684;
&#x95EE;&#x9898;3&#xFF1A;&#x5982;&#x679C;&#x5BF9;&#x8C61;&#x7684;&#x5C5E;&#x6027;&#x503C;&#x662F;&#x6570;&#x7EC4;&#x7C7B;&#x578B;&#xFF0C;&#x90A3;&#x4E48;&#x4E5F;&#x4E0D;&#x4F1A;&#x89E6;&#x53D1;&#x54CD;&#x5E94;&#x5F0F;&#x3002;&#x8FD9;&#x65F6;&#x5019;&#x8981;&#x5BF9;&#x6570;&#x7EC4;&#x4E0A;&#x7684;&#x65B9;&#x6CD5;&#x8FDB;&#x884C;&#x91CD;&#x5199; push shift&#x3002;&#x3002;&#x3002;
&#x4F8B;&#x5982;&#xFF1A;</p>
<pre class="language-"><code class="lang-js"><span class="token keyword">let</span> oldproto <span class="token operator">=</span> <span class="token class-name">Array</span><span class="token punctuation">.</span>prototype<span class="token punctuation">;</span>
<span class="token keyword">let</span> proto <span class="token operator">=</span> Object<span class="token punctuation">.</span><span class="token function">create</span><span class="token punctuation">(</span>oldproto<span class="token punctuation">)</span>
<span class="token punctuation">[</span><span class="token string">&apos;push&apos;</span><span class="token punctuation">,</span><span class="token string">&apos;shift&apos;</span><span class="token punctuation">,</span><span class="token string">&apos;unshift&apos;</span><span class="token punctuation">]</span><span class="token punctuation">.</span><span class="token function">forEach</span><span class="token punctuation">(</span><span class="token keyword">function</span><span class="token punctuation">(</span><span class="token parameter">method</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
    proto<span class="token punctuation">[</span>method<span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token keyword">function</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
        <span class="token function">updateView</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

        oldproto<span class="token punctuation">[</span>method<span class="token punctuation">]</span><span class="token punctuation">.</span><span class="token function">call</span><span class="token punctuation">(</span><span class="token keyword">this</span><span class="token punctuation">,</span><span class="token operator">...</span>arguments<span class="token punctuation">)</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span>
<span class="token comment">//&#x5728;observe&#x65B9;&#x6CD5;</span>
<span class="token keyword">if</span><span class="token punctuation">(</span>Array<span class="token punctuation">.</span><span class="token function">isArray</span><span class="token punctuation">(</span>obj<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
    obj<span class="token punctuation">.</span>__proto__ <span class="token operator">=</span> proto<span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre>
<h2 id="proxy&#x57FA;&#x7840;"><a name="proxy&#x57FA;&#x7840;" class="anchor-navigation-ex-anchor" href="#proxy&#x57FA;&#x7840;"><i class="fa fa-link" aria-hidden="true"></i></a>proxy&#x57FA;&#x7840;</h2>
<p>&#x5173;&#x4E8E; Proxy &#x7684;&#x57FA;&#x7840;&#x77E5;&#x8BC6;&#xFF0C;&#x53EF;&#x4EE5;&#x53BB;MDN&#x5B66;&#x4E60;&#x3002;<a href="https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Proxy" target="_blank">&#x94FE;&#x63A5;</a></p>
<h2 id="&#x5B9E;&#x73B0;&#x76EE;&#x6807;-"><a name="&#x5B9E;&#x73B0;&#x76EE;&#x6807;-" class="anchor-navigation-ex-anchor" href="#&#x5B9E;&#x73B0;&#x76EE;&#x6807;-"><i class="fa fa-link" aria-hidden="true"></i></a>&#x5B9E;&#x73B0;&#x76EE;&#x6807; </h2>
<p>&#x5B9E;&#x73B0;&#x4E00;&#x4E2A;&#x54CD;&#x5E94;&#x5F0F;&#x7684;&#x6838;&#x5FC3;&#x51FD;&#x6570;&#x547D;&#x540D;&#x4E3A; reactive&#xFF0C; &#x8FD9;&#x4E2A;&#x51FD;&#x6570;&#x8FD4;&#x56DE;&#x4E00;&#x4E2A;&#x88AB;&#x4EE3;&#x7406;&#x4E4B;&#x540E;&#x7684;&#x7ED3;&#x679C;&#xFF0C;&#x53EF;&#x4EE5;&#x901A;&#x8FC7;&#x64CD;&#x4F5C;&#x8FD9;&#x4E2A;&#x8FD4;&#x56DE;&#x7ED3;&#x679C;&#x6765;&#x89E6;&#x53D1;&#x54CD;&#x5E94;&#x5F0F;&#x3002;
&#x6BD4;&#x5982;&#x5B9E;&#x73B0;&#x4E00;&#x4E2A;&#x6709; name &#x5C5E;&#x6027;&#x7684;&#x5BF9;&#x8C61; obj&#xFF0C;&#x6211;&#x4EEC;&#x5E0C;&#x671B;&#x7ECF;&#x8FC7; reactive &#x51FD;&#x6570;&#x5904;&#x7406;&#x4E4B;&#x540E;&#x8FD4;&#x56DE;&#x4E00;&#x4E2A;&#x65B0;&#x5BF9;&#x8C61; proxyObj&#xFF0C;&#x6211;&#x4EEC;&#x50CF;&#x64CD;&#x4F5C; obj &#x4E00;&#x6837;&#x64CD;&#x4F5C; proxyObj &#x5BF9;&#x8C61;&#x3002;</p>
<pre class="language-"><code class="lang-javascript"><span class="token keyword">let</span> obj <span class="token operator">=</span> <span class="token punctuation">{</span>
    name<span class="token punctuation">:</span> <span class="token string">&apos;test name&apos;</span>
<span class="token punctuation">}</span>

<span class="token keyword">let</span> proxyObj <span class="token operator">=</span> <span class="token function">reactive</span><span class="token punctuation">(</span>obj<span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre>
<p>&#x4F8B;&#x5982;&#xFF1A;&#x5F53;&#x4FEE;&#x6539; proxyObj.name &#x65F6;&#x4F1A;&#x89E6;&#x53D1;&#x54CD;&#x5E94;&#x5F0F;&#x3002;</p>
<p>&#x5176;&#x6B21;&#xFF1A;&#x8BBE;&#x7F6E;&#x4E00;&#x4E2A;&#x51FD;&#x6570;&#x7528;&#x6765;&#x6A21;&#x62DF;&#x54CD;&#x5E94;&#x5F0F;&#x8FC7;&#x7A0B;&#xFF0C;&#x8FD9;&#x91CC;&#x7701;&#x7565;&#x6389;&#x66F4;&#x65B0;dom&#x7684;&#x64CD;&#x4F5C;,&#x7528;&#x6253;&#x5370;&#x4E00;&#x53E5;&#x8BDD;&#x6765;&#x4EE3;&#x66FF;&#x6A21;&#x62DF;&#x4E00;&#x4E0B;&#x3002;</p>
<pre class="language-"><code class="lang-javascript"><span class="token comment">// &#x63D0;&#x793A;&#x89C6;&#x56FE;&#x9700;&#x8981;&#x66F4;&#x65B0;</span>
<span class="token keyword">function</span> <span class="token function">trigger</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token string">&apos;&#x89C6;&#x56FE;&#x9700;&#x8981;&#x66F4;&#x65B0;&apos;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre>
<h2 id="&#x5B9E;&#x73B0;-reactive-&#x51FD;&#x6570;"><a name="&#x5B9E;&#x73B0;-reactive-&#x51FD;&#x6570;" class="anchor-navigation-ex-anchor" href="#&#x5B9E;&#x73B0;-reactive-&#x51FD;&#x6570;"><i class="fa fa-link" aria-hidden="true"></i></a>&#x5B9E;&#x73B0; reactive &#x51FD;&#x6570;</h2>
<h3 id="&#x9700;&#x8981;&#x4E00;&#x4E2A;&#x8F85;&#x52A9;&#x51FD;&#x6570;"><a name="&#x9700;&#x8981;&#x4E00;&#x4E2A;&#x8F85;&#x52A9;&#x51FD;&#x6570;" class="anchor-navigation-ex-anchor" href="#&#x9700;&#x8981;&#x4E00;&#x4E2A;&#x8F85;&#x52A9;&#x51FD;&#x6570;"><i class="fa fa-link" aria-hidden="true"></i></a>&#x9700;&#x8981;&#x4E00;&#x4E2A;&#x8F85;&#x52A9;&#x51FD;&#x6570;</h3>
<p>&#x9996;&#x5148;&#x660E;&#x786E; reactive &#x51FD;&#x6570;&#x63A5;&#x6536;&#x4E00;&#x4E2A;&#x53C2;&#x6570;&#xFF0C;&#x9700;&#x8981;&#x5BF9;&#x8FD9;&#x4E2A;&#x53C2;&#x6570;&#x8FDB;&#x884C;&#x4EE3;&#x7406;&#xFF0C;&#x5E76;&#x8FD4;&#x56DE;&#x4EE3;&#x7406;&#x540E;&#x7684;&#x7ED3;&#x679C;&#x3002;&#x5982;&#x679C;&#x53C2;&#x6570;&#x662F;&#x5BF9;&#x8C61;&#x624D;&#x9700;&#x8981;&#x4EE3;&#x7406;&#xFF0C;&#x5426;&#x5219;&#x76F4;&#x63A5;&#x8FD4;&#x56DE;&#x3002;</p>
<p>&#x8FD9;&#x91CC;&#x9700;&#x8981;&#x5EFA;&#x7ACB;&#x4E00;&#x4E2A;&#x8F85;&#x52A9;&#x51FD;&#x6570;&#x7528;&#x6765;&#x5224;&#x65AD;&#x4E00;&#x4E2A;&#x53D8;&#x91CF;&#x662F;&#x5426;&#x662F;&#x5BF9;&#x8C61;&#xFF1A;</p>
<pre class="language-"><code class="lang-javascript"><span class="token keyword">function</span> <span class="token function">isObject</span><span class="token punctuation">(</span><span class="token parameter">param</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">return</span> <span class="token keyword">typeof</span> param <span class="token operator">===</span> <span class="token string">&apos;object&apos;</span> <span class="token operator">&amp;&amp;</span> param <span class="token operator">!==</span> <span class="token keyword">null</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre>
<h3 id="&#x4E3B;&#x4F53;&#x51FD;&#x6570;"><a name="&#x4E3B;&#x4F53;&#x51FD;&#x6570;" class="anchor-navigation-ex-anchor" href="#&#x4E3B;&#x4F53;&#x51FD;&#x6570;"><i class="fa fa-link" aria-hidden="true"></i></a>&#x4E3B;&#x4F53;&#x51FD;&#x6570;</h3>
<p>&#x4F7F;&#x7528; Proxy &#x65F6;&#x9700;&#x8981;&#x5B9A;&#x4E49;&#x4E00;&#x4E2A;&#x4EE3;&#x7406;&#x5BF9;&#x8C61; handler &#x6765;&#x5BF9;&#x76EE;&#x6807;&#x8FDB;&#x884C;&#x4EE3;&#x7406;&#x64CD;&#x4F5C;&#xFF0C;&#x8FD9;&#x4E2A;&#x5BF9;&#x8C61;&#x4E3B;&#x8981;&#x6709;&#x4E24;&#x4E2A;&#x65B9;&#x6CD5;&#xFF0C;&#x5373; get &#x548C; set, &#x5206;&#x522B;&#x4E3A; &#x83B7;&#x53D6;&#x548C;&#x8BBE;&#x7F6E;&#x5C5E;&#x6027;&#x503C;&#x7684;&#x65F6;&#x5019;&#x89E6;&#x53D1;&#x3002;&#x540C;&#x65F6;&#x5728;&#x5185;&#x90E8;&#x7684;&#x5B9E;&#x73B0;&#x9700;&#x8981;&#x5229;&#x7528;&#x5230; Reflect&#x5BF9;&#x8C61;&#xFF0C; &#x8BE6;&#x60C5;&#x89C1;&#x4E0B;&#x9762;&#x7684;&#x4EE3;&#x7801;&#x3002;</p>
<pre class="language-"><code class="lang-javascript"><span class="token comment">/* &#x8FD4;&#x56DE;&#x4E00;&#x4E2A;&#x88AB;&#x4EE3;&#x7406;&#x540E;&#x7684;&#x7ED3;&#x679C;&#xFF0C;&#x901A;&#x8FC7;&#x64CD;&#x4F5C;&#x8FD9;&#x4E2A;&#x7ED3;&#x679C;&#x53EF;&#x4EE5;&#x6765;&#x5B9E;&#x73B0;&#x54CD;&#x5E94;&#x5F0F;, &#x4F8B;&#x5982;&#x89C6;&#x56FE;&#x66F4;&#x65B0; */</span>
<span class="token keyword">function</span> <span class="token function">reactive</span><span class="token punctuation">(</span><span class="token parameter">target</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token comment">// &#x5982;&#x679C;&#x662F;&#x4E2A;&#x5BF9;&#x8C61;&#xFF0C;&#x5219;&#x8FD4;&#x56DE;&#x88AB;&#x4EE3;&#x7406;&#x540E;&#x7684;&#x7ED3;&#x679C;&#xFF0C;&#x5982;&#x679C;&#x4E0D;&#x662F;&#x5219;&#x76F4;&#x63A5;&#x8FD4;&#x56DE;</span>
    <span class="token keyword">if</span><span class="token punctuation">(</span><span class="token operator">!</span><span class="token function">isObject</span><span class="token punctuation">(</span>target<span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">return</span> target<span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    <span class="token comment">// &#x9700;&#x8981;&#x5B9A;&#x4E49;&#x4E00;&#x4E2A;&#x4EE3;&#x7406;&#x5BF9;&#x8C61;&#x6765;&#x5BF9; target &#x8FDB;&#x884C;&#x4EE3;&#x7406;&#x64CD;&#x4F5C;</span>
    <span class="token comment">// &#x8FD9;&#x4E2A;&#x5BF9;&#x8C61;&#x4E3B;&#x8981;&#x6709;&#x4E24;&#x4E2A;&#x65B9;&#x6CD5;&#xFF0C;&#x5373; get &#x548C; set</span>
    <span class="token keyword">const</span> handler <span class="token operator">=</span> <span class="token punctuation">{</span>
        <span class="token keyword">get</span><span class="token punctuation">(</span>target<span class="token punctuation">,</span> key<span class="token punctuation">,</span> receiver<span class="token punctuation">)</span> <span class="token punctuation">{</span>

            <span class="token keyword">return</span> Reflect<span class="token punctuation">.</span><span class="token function">get</span><span class="token punctuation">(</span>target<span class="token punctuation">,</span> key<span class="token punctuation">,</span> receiver<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// &#x76F8;&#x5F53;&#x4E8E; return target[key]</span>
        <span class="token punctuation">}</span><span class="token punctuation">,</span>
        <span class="token keyword">set</span><span class="token punctuation">(</span>target<span class="token punctuation">,</span> key<span class="token punctuation">,</span> value<span class="token punctuation">,</span> receiver<span class="token punctuation">)</span> <span class="token punctuation">{</span>
            <span class="token function">trigger</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token keyword">return</span> Reflect<span class="token punctuation">.</span><span class="token function">set</span><span class="token punctuation">(</span>target<span class="token punctuation">,</span> key<span class="token punctuation">,</span> value<span class="token punctuation">,</span> receiver<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// &#x76F8;&#x5F53;&#x4E8E; target[key] = value</span>
        <span class="token punctuation">}</span>
    <span class="token punctuation">}</span><span class="token punctuation">;</span>

    <span class="token comment">// &#x5229;&#x7528; Proxy &#x6765;&#x4EE3;&#x7406;&#x8FD9;&#x4E2A;&#x5BF9;&#x8C61;&#x5C5E;&#x6027;</span>
    <span class="token keyword">let</span> observed <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Proxy</span><span class="token punctuation">(</span>target<span class="token punctuation">,</span> handler<span class="token punctuation">)</span><span class="token punctuation">;</span>

    <span class="token keyword">return</span> observed<span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre>
<p>&#x73B0;&#x5728;&#x6211;&#x4EEC;&#x4FEE;&#x6539; proxyObj &#x7684;name&#x5C5E;&#x6027;&#xFF0C;&#x53D1;&#x73B0;&#x4F1A;&#x89E6;&#x53D1;&#x4E86;&#x6700;&#x57FA;&#x672C;&#x7684;&#x54CD;&#x5E94;&#x5F0F;&#xFF1A;</p>
<pre class="language-"><code class="lang-javascript"><span class="token keyword">let</span> obj <span class="token operator">=</span> <span class="token punctuation">{</span>
    name<span class="token punctuation">:</span> <span class="token string">&apos;jjjs&apos;</span>
<span class="token punctuation">}</span>

<span class="token keyword">let</span> proxyObj <span class="token operator">=</span> <span class="token function">reactive</span><span class="token punctuation">(</span>obj<span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token comment">// &#x4FEE;&#x6539; name &#x5C5E;&#x6027;&#xFF0C;&#x53D1;&#x73B0;&#x53EF;&#x4EE5;&#x76D1;&#x63A7;&#x5230;</span>
proxyObj<span class="token punctuation">.</span>name <span class="token operator">=</span> <span class="token string">&apos;new Name&apos;</span><span class="token punctuation">;</span>
console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>proxyObj<span class="token punctuation">.</span>name<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment">//&#x7ED3;&#x679C;&#x6253;&#x5370;&#xFF1A;</span>
<span class="token comment">//&#x89C6;&#x56FE;&#x9700;&#x8981;&#x66F4;&#x65B0;</span>
<span class="token comment">//new Name</span>
</code></pre>
<p>&#x800C;&#x4E14;&#x53EF;&#x4EE5;&#x5BF9;&#x672C;&#x6765;&#x4E0D;&#x5B58;&#x5728;&#x7684;&#x5C5E;&#x6027;&#x8FDB;&#x884C;&#x76D1;&#x63A7;&#xFF1A;</p>
<pre class="language-"><code class="lang-javascript">proxyObj<span class="token punctuation">.</span>age <span class="token operator">=</span> <span class="token number">6</span><span class="token punctuation">;</span>
console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>proxyObj<span class="token punctuation">.</span>age<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment">//&#x89C6;&#x56FE;&#x9700;&#x8981;&#x66F4;&#x65B0;</span>
<span class="token comment">//6</span>
</code></pre>
<p>&#x4F46;&#x662F;&#x5F53;&#x6211;&#x4EEC;&#x60F3;&#x5904;&#x7406;&#x4E00;&#x4E2A;&#x6570;&#x7EC4;&#x65F6;&#xFF0C;&#x5374;&#x53D1;&#x73B0;&#x4F1A;&#x89E6;&#x53D1;&#x4E24;&#x6B21;&#x89C6;&#x56FE;&#x66F4;&#x65B0;&#x63D0;&#x793A;&#xFF1A;</p>
<pre class="language-"><code class="lang-javascript"><span class="token keyword">let</span> array <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token number">2</span><span class="token punctuation">,</span><span class="token number">3</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
<span class="token keyword">let</span> obArray <span class="token operator">=</span> <span class="token function">reactive</span><span class="token punctuation">(</span>array<span class="token punctuation">)</span><span class="token punctuation">;</span>
obArray<span class="token punctuation">.</span><span class="token function">push</span><span class="token punctuation">(</span><span class="token number">4</span><span class="token punctuation">)</span>  <span class="token comment">// --&#x7ED3;&#x679C;&#x4F1A;&#x51FA;&#x73B0;2&#x6B21;   &#x89C6;&#x56FE;&#x9700;&#x8981;&#x66F4;&#x65B0;</span>
</code></pre>
<p>&#x8FD9;&#x4E2A;&#x65F6;&#x5019;&#x5BF9;reactive&#x4EE3;&#x7801;&#x8FDB;&#x884C;&#x4FEE;&#x6539;&#xFF0C;&#x8F93;&#x51FA;set&#x52A8;&#x4F5C;&#x4E2D;&#x6BCF;&#x6B21;&#x7684; key, &#x89C2;&#x5BDF;&#x662F;&#x54EA;&#x4E2A;&#x952E;&#x4F7F;&#x5F97;&#x51FD;&#x6570;&#x89E6;&#x53D1;&#x4E86;&#x4E24;&#x6B21;</p>
<pre class="language-"><code class="lang-javascript"><span class="token keyword">function</span> <span class="token function">reactive</span><span class="token punctuation">(</span><span class="token parameter">target</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">const</span> handler <span class="token operator">=</span> <span class="token punctuation">{</span>
    <span class="token comment">// ...</span>
        <span class="token keyword">set</span><span class="token punctuation">(</span>target<span class="token punctuation">,</span> key<span class="token punctuation">,</span> value<span class="token punctuation">,</span> receiver<span class="token punctuation">)</span> <span class="token punctuation">{</span>
            <span class="token function">trigger</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>key<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// &#x8F93;&#x51FA;&#x53D8;&#x52A8;&#x7684; key  &#x662F;3 &#x548C;length</span>
            <span class="token keyword">return</span> Reflect<span class="token punctuation">.</span><span class="token function">set</span><span class="token punctuation">(</span>target<span class="token punctuation">,</span> key<span class="token punctuation">,</span> value<span class="token punctuation">,</span> receiver<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// &#x76F8;&#x5F53;&#x4E8E; target[key] = value</span>
        <span class="token punctuation">}</span>
    <span class="token punctuation">}</span><span class="token punctuation">;</span>
    <span class="token comment">// ...</span>
<span class="token punctuation">}</span>
</code></pre>
<p>&#x53EF;&#x4EE5;&#x770B;&#x51FA;&#x5F53;&#x76D1;&#x89C6;&#x6570;&#x7EC4;&#x65F6;&#xFF0C;&#x6570;&#x7EC4;&#x4E0B;&#x6807;&#x7684;&#x66F4;&#x65B0;&#x4F1A;&#x89E6;&#x53D1;&#x4E00;&#x6B21;&#xFF0C;&#x800C;&#x6570;&#x7EC4;length&#x7684;&#x66F4;&#x65B0;&#x4E5F;&#x4F1A;&#x8FDB;&#x884C;&#x89E6;&#x53D1;&#xFF0C;&#x8FD9;&#x5C31;&#x662F;&#x4E8C;&#x6B21;&#x89E6;&#x53D1;&#x7684;&#x539F;&#x56E0;&#x3002;
&#x4F46;&#x6211;&#x4EEC;&#x662F;&#x4E0D;&#x9700;&#x8981;&#x5728; length &#x66F4;&#x65B0;&#x65F6;&#x5BF9;&#x89C6;&#x56FE;&#x8FDB;&#x884C;&#x66F4;&#x65B0;&#x7684;&#xFF0C;&#x6240;&#x4EE5;&#x9700;&#x8981;&#x5BF9;&#x8FD9;&#x91CC;&#x7684;&#x903B;&#x8F91;&#x8FDB;&#x884C;&#x4FEE;&#x6539;&#xFF1A;&#x53EA;&#x5BF9;&#x79C1;&#x6709;&#x5C5E;&#x6027;&#x7684;&#x4FEE;&#x6539;&#x52A8;&#x4F5C;&#x89E6;&#x53D1;&#x89C6;&#x56FE;&#x66F4;&#x65B0;&#x3002;</p>
<pre class="language-"><code class="lang-javascript"><span class="token keyword">function</span> <span class="token function">reactive</span><span class="token punctuation">(</span><span class="token parameter">target</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">const</span> handler <span class="token operator">=</span> <span class="token punctuation">{</span>
        <span class="token comment">// ...</span>
        <span class="token keyword">set</span><span class="token punctuation">(</span>target<span class="token punctuation">,</span> key<span class="token punctuation">,</span> value<span class="token punctuation">,</span> receiver<span class="token punctuation">)</span> <span class="token punctuation">{</span>
            <span class="token comment">// &#x53EA;&#x5BF9;&#x79C1;&#x6709;&#x5C5E;&#x6027;&#x7684;&#x4FEE;&#x6539;&#x52A8;&#x4F5C;&#x89E6;&#x53D1;&#x89C6;&#x56FE;&#x66F4;&#x65B0;</span>
            <span class="token keyword">if</span><span class="token punctuation">(</span><span class="token operator">!</span>target<span class="token punctuation">.</span><span class="token function">hasOwnProperty</span><span class="token punctuation">(</span>key<span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
                <span class="token function">trigger</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
                console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>key<span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token punctuation">}</span>
            <span class="token keyword">return</span> Reflect<span class="token punctuation">.</span><span class="token function">set</span><span class="token punctuation">(</span>target<span class="token punctuation">,</span> key<span class="token punctuation">,</span> value<span class="token punctuation">,</span> receiver<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// &#x76F8;&#x5F53;&#x4E8E; target[key] = value</span>
        <span class="token punctuation">}</span>
    <span class="token punctuation">}</span><span class="token punctuation">;</span>
    <span class="token comment">// ...</span>
<span class="token punctuation">}</span>
</code></pre>
<p>&#x5F53;&#x9700;&#x8981;&#x5BF9;&#x5D4C;&#x5957;&#x7684;&#x5BF9;&#x8C61;&#x8FDB;&#x884C;&#x83B7;&#x53D6;&#x65F6;&#xFF0C;&#x4F8B;&#x5982;&#xFF1A;</p>
<pre class="language-"><code class="lang-javascript"><span class="token comment">// &#x5BF9;&#x4E8E;&#x5D4C;&#x5957;&#x7684;&#x5BF9;&#x8C61;</span>
<span class="token keyword">var</span> obj <span class="token operator">=</span> <span class="token punctuation">{</span>
    name<span class="token punctuation">:</span> <span class="token string">&apos;jjjs&apos;</span><span class="token punctuation">,</span>
    array<span class="token punctuation">:</span> <span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token number">2</span><span class="token punctuation">,</span><span class="token number">3</span><span class="token punctuation">]</span>
<span class="token punctuation">}</span>

<span class="token keyword">var</span> proxyObj <span class="token operator">=</span> <span class="token function">reactive</span><span class="token punctuation">(</span>obj<span class="token punctuation">)</span><span class="token punctuation">;</span>
proxyObj<span class="token punctuation">.</span>array<span class="token punctuation">.</span><span class="token function">push</span><span class="token punctuation">(</span><span class="token number">4</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre>
<p>&#x6B64;&#x65F6;&#x4F1A;&#x53D1;&#x73B0;&#xFF0C;&#x5E76;&#x4E0D;&#x4F1A;&#x89E6;&#x53D1;&#x89C6;&#x56FE;&#x9700;&#x8981;&#x66F4;&#x65B0;&#x7684;&#x63D0;&#x793A;&#xFF0C;&#x8FD9;&#x65F6;&#x9700;&#x8981;&#x5BF9;&#x5BF9;&#x8C61;&#x8FDB;&#x884C;&#x9012;&#x5F52;&#x5904;&#x7406;&#xFF1A;</p>
<pre class="language-"><code class="lang-javascript"><span class="token keyword">function</span> <span class="token function">reactive</span><span class="token punctuation">(</span><span class="token parameter">target</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
<span class="token comment">// ...</span>
    <span class="token keyword">const</span> handler <span class="token operator">=</span> <span class="token punctuation">{</span>
        <span class="token keyword">get</span><span class="token punctuation">(</span>target<span class="token punctuation">,</span> key<span class="token punctuation">,</span> receiver<span class="token punctuation">)</span> <span class="token punctuation">{</span>
            <span class="token keyword">const</span> proxyTarget <span class="token operator">=</span>  Reflect<span class="token punctuation">.</span><span class="token function">get</span><span class="token punctuation">(</span>target<span class="token punctuation">,</span> key<span class="token punctuation">,</span> receiver<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// &#x76F8;&#x5F53;&#x4E8E;&#x83B7;&#x53D6; target[key]</span>
            <span class="token keyword">if</span><span class="token punctuation">(</span><span class="token function">isObject</span><span class="token punctuation">(</span>target<span class="token punctuation">[</span>key<span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token comment">// &#x5BF9;&#x4E8E;&#x5BF9;&#x8C61;&#x8FDB;&#x884C;&#x9012;&#x5F52;</span>
                <span class="token keyword">return</span> <span class="token function">reactive</span><span class="token punctuation">(</span>proxyTarget<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// &#x9012;&#x5F52;</span>
            <span class="token punctuation">}</span>

            <span class="token keyword">return</span> proxyTarget<span class="token punctuation">;</span>
        <span class="token punctuation">}</span><span class="token punctuation">,</span>
        <span class="token comment">// ...</span>
    <span class="token punctuation">}</span><span class="token punctuation">;</span>
<span class="token comment">// ...</span>
<span class="token punctuation">}</span>
</code></pre>
<p>&#x7136;&#x800C;&#xFF0C;&#x5F53;&#x591A;&#x6B21;&#x83B7;&#x53D6;&#x4EE3;&#x7406;&#x7ED3;&#x679C;&#x65F6;&#xFF0C;&#x4F1A;&#x51FA;&#x73B0;&#x591A;&#x6B21;&#x89E6;&#x53D1;&#x4EE3;&#x7406;&#x7684;&#x60C5;&#x51B5;&#xFF1A;</p>
<pre class="language-"><code class="lang-javascript"><span class="token keyword">function</span> <span class="token function">reactive</span><span class="token punctuation">(</span><span class="token parameter">target</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
<span class="token comment">// ...</span>
    console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token string">&apos;&#x8D70;&#x4EE3;&#x7406;&apos;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

    <span class="token comment">// &#x5229;&#x7528; Proxy &#x6765;&#x4EE3;&#x7406;&#x8FD9;&#x4E2A;&#x5BF9;&#x8C61;&#x5C5E;&#x6027;</span>
    <span class="token keyword">let</span> observed <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Proxy</span><span class="token punctuation">(</span>target<span class="token punctuation">,</span> handler<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">return</span> observed<span class="token punctuation">;</span>
<span class="token punctuation">}</span>

<span class="token comment">// &#x591A;&#x6B21;&#x83B7;&#x53D6;&#x4EE3;&#x7406;&#x7ED3;&#x679C;</span>
<span class="token keyword">var</span> proxyObj <span class="token operator">=</span> <span class="token function">reactive</span><span class="token punctuation">(</span>obj<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">var</span> proxyObj <span class="token operator">=</span> <span class="token function">reactive</span><span class="token punctuation">(</span>obj<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">var</span> proxyObj <span class="token operator">=</span> <span class="token function">reactive</span><span class="token punctuation">(</span>obj<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">var</span> proxyObj <span class="token operator">=</span> <span class="token function">reactive</span><span class="token punctuation">(</span>obj<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment">//=&gt;&#x7ED3;&#x679C;&#x8F93;&#x51FA; 4&#x6B21; &#x8D70;&#x4EE3;&#x7406;</span>

<span class="token comment">//&#x4EE3;&#x7406;&#x88AB;&#x4EE3;&#x7406;&#x8FC7;&#x7684;&#x5BF9;&#x8C61;</span>
<span class="token function">reactive</span><span class="token punctuation">(</span>proxyObj<span class="token punctuation">)</span>
<span class="token function">reactive</span><span class="token punctuation">(</span>proxyObj<span class="token punctuation">)</span>
<span class="token function">reactive</span><span class="token punctuation">(</span>proxyObj<span class="token punctuation">)</span>
<span class="token function">reactive</span><span class="token punctuation">(</span>proxyObj<span class="token punctuation">)</span>
</code></pre>
<p>&#x4E0A;&#x8FF0;&#x4E24;&#x79CD;&#x60C5;&#x51B5;&#x662F;&#x6211;&#x4EEC;&#x4E0D;&#x5E0C;&#x671B;&#x6709;&#x7684;&#xFF0C;&#x6211;&#x4EEC;&#x5E0C;&#x671B;&#x5BF9;&#x540C;&#x4E00;&#x4E2A;&#x5BF9;&#x8C61;&#x4EC5;&#x505A;&#x4E00;&#x6B21;&#x4EE3;&#x7406;&#x3002;&#x8FD9;&#x4E2A;&#x65F6;&#x5019;&#xFF0C;&#x6211;&#x4EEC;&#x9700;&#x8981;&#x5BF9;&#x5DF2;&#x7ECF;&#x4EE3;&#x7406;&#x8FC7;&#x7684;&#x5BF9;&#x8C61;&#x8FDB;&#x884C;&#x7F13;&#x5B58;&#xFF0C;&#x4E00;&#x6B21;&#x5728;&#x8FDB;&#x884C;&#x4EE3;&#x7406;&#x4E4B;&#x524D;&#x67E5;&#x8BE2;&#x7F13;&#x5B58;&#x5224;&#x65AD;&#x662F;&#x5426;&#x5DF2;&#x7ECF;&#x7ECF;&#x8FC7;&#x4E86;&#x4EE3;&#x7406;&#xFF0C;&#x53EA;&#x6709;&#x6CA1;&#x6709;&#x7ECF;&#x8FC7;&#x4EE3;&#x7406;&#x7684;&#x5BF9;&#x8C61;&#x624D;&#x8D70;&#x4E00;&#x6B21;&#x4EE3;&#x7406;&#x3002;</p>
<p>&#x6700;&#x9002;&#x5408;&#x5F53;&#x505A;&#x7F13;&#x5B58;&#x5BB9;&#x5668;&#x7684;&#x5BF9;&#x8C61;&#x662F; WeakMap&#xFF0C; &#x8FD9;&#x662F;&#x7531;&#x4E8E;&#x5B83;&#x5BF9;&#x4E8E;&#x5BF9;&#x8C61;&#x7684;&#x5F31;&#x5F15;&#x7528;&#x7279;&#x6027;&#xFF0C;vue3&#x4E2D;&#x4E5F;&#x662F;&#x4F7F;&#x7528;&#x4E86;weakmap&#x6765;&#x7EF4;&#x62A4;&#x3002;</p>
<blockquote>
<p>Q:&#x4E3A;&#x4EC0;&#x4E48;&#x7528;weekmap&#x800C;&#x4E0D;&#x662F;map?
A:weakmap&#x662F;&#x5F31;&#x7C7B;&#x578B;&#x53EF;&#x4EE5;&#x76F4;&#x63A5;&#x88AB;&#x56DE;&#x6536;</p>
</blockquote>
<p>&#x73B0;&#x5728;&#x5BF9;&#x4EE3;&#x7801;&#x8FDB;&#x884C;&#x4FEE;&#x6539;&#xFF0C;&#x589E;&#x52A0;&#x7F13;&#x5B58;&#x5BF9;&#x8C61;&#xFF1A;</p>
<pre class="language-"><code class="lang-javascript"><span class="token keyword">const</span> toProxy <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">WeakMap</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// &#x7528;&#x6765;&#x4FDD;&#x5B58;   &#x539F;&#x5BF9;&#x8C61;=&gt;&#x4EE3;&#x7406;&#x540E;&#x7684;&#x5BF9;&#x8C61;</span>
<span class="token keyword">const</span> toRaw <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">WeakMap</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>    <span class="token comment">//&#x4EE3;&#x7406;&#x540E;&#x7684;&#x5BF9;&#x8C61;=&gt;&#x539F;&#x5BF9;&#x8C61;</span>
<span class="token keyword">function</span> <span class="token function">reactive</span><span class="token punctuation">(</span><span class="token parameter">target</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token comment">// ...</span>
    <span class="token keyword">if</span><span class="token punctuation">(</span>toProxy<span class="token punctuation">.</span><span class="token function">get</span><span class="token punctuation">(</span>target<span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token comment">// &#x5224;&#x65AD;&#x5BF9;&#x8C61;&#x662F;&#x5426;&#x5DF2;&#x7ECF;&#x88AB;&#x4EE3;&#x7406;&#x4E86;</span>
        <span class="token keyword">return</span> toProxy<span class="token punctuation">.</span><span class="token function">get</span><span class="token punctuation">(</span>target<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    <span class="token comment">// ...</span>
    console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token string">&apos;&#x8D70;&#x4EE3;&#x7406;&apos;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token comment">// &#x5229;&#x7528; Proxy &#x6765;&#x4EE3;&#x7406;&#x8FD9;&#x4E2A;&#x5BF9;&#x8C61;&#x5C5E;&#x6027;</span>
    <span class="token keyword">let</span> observed <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Proxy</span><span class="token punctuation">(</span>target<span class="token punctuation">,</span> handler<span class="token punctuation">)</span><span class="token punctuation">;</span>

    toProxy<span class="token punctuation">.</span><span class="token function">set</span><span class="token punctuation">(</span>target<span class="token punctuation">,</span> observed<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// &#x4FDD;&#x5B58;&#x5DF2;&#x7ECF;&#x4EE3;&#x7406;&#x4E86;&#x7684;&#x5BF9;&#x8C61;</span>

    <span class="token keyword">return</span> observed<span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre>
<p>&#x53D1;&#x73B0;&#x5BF9;&#x540C;&#x4E00;&#x4E2A;&#x5BF9;&#x8C61;&#x53EA;&#x8D70;&#x4E86;&#x4E00;&#x6B21;&#x4EE3;&#x7406;&#xFF0C;&#x8FD9;&#x6B63;&#x662F;&#x6211;&#x4EEC;&#x671F;&#x671B;&#x7684;&#x7ED3;&#x679C;&#x3002;</p>
<h3 id="&#x5B8C;&#x6574;&#x4EE3;&#x7801;"><a name="&#x5B8C;&#x6574;&#x4EE3;&#x7801;" class="anchor-navigation-ex-anchor" href="#&#x5B8C;&#x6574;&#x4EE3;&#x7801;"><i class="fa fa-link" aria-hidden="true"></i></a>&#x5B8C;&#x6574;&#x4EE3;&#x7801;</h3>
<pre class="language-"><code class="lang-javascript"><span class="token comment">/* demo.js */</span>

<span class="token comment">// &#x5B9A;&#x4E49;&#x4E00;&#x4E2A;&#x7F13;&#x5B58;&#x5BF9;&#x8C61;</span>
<span class="token keyword">const</span> toProxy <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">WeakMap</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// &#x4FDD;&#x5B58;&#x4EE3;&#x7406;&#x540E;&#x7684;&#x5BF9;&#x8C61;</span>

<span class="token comment">/* &#x8FD4;&#x56DE;&#x4E00;&#x4E2A;&#x88AB;&#x4EE3;&#x7406;&#x540E;&#x7684;&#x7ED3;&#x679C;&#xFF0C;&#x901A;&#x8FC7;&#x64CD;&#x4F5C;&#x8FD9;&#x4E2A;&#x7ED3;&#x679C;&#x53EF;&#x4EE5;&#x6765;&#x5B9E;&#x73B0;&#x54CD;&#x5E94;&#x5F0F;, &#x4F8B;&#x5982;&#x89C6;&#x56FE;&#x66F4;&#x65B0; */</span>
<span class="token keyword">function</span> <span class="token function">reactive</span><span class="token punctuation">(</span><span class="token parameter">target</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token comment">// &#x5982;&#x679C;&#x662F;&#x4E2A;&#x5BF9;&#x8C61;&#xFF0C;&#x5219;&#x8FD4;&#x56DE;&#x88AB;&#x4EE3;&#x7406;&#x540E;&#x7684;&#x7ED3;&#x679C;&#xFF0C;&#x5982;&#x679C;&#x4E0D;&#x662F;&#x5219;&#x76F4;&#x63A5;&#x8FD4;&#x56DE;</span>
    <span class="token keyword">if</span><span class="token punctuation">(</span><span class="token operator">!</span><span class="token function">isObject</span><span class="token punctuation">(</span>target<span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">return</span> target<span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    <span class="token comment">// &#x5224;&#x65AD;&#x5BF9;&#x8C61;&#x662F;&#x5426;&#x5DF2;&#x7ECF;&#x88AB;&#x4EE3;&#x7406;&#x4E86;</span>
    <span class="token keyword">if</span><span class="token punctuation">(</span>toProxy<span class="token punctuation">.</span><span class="token function">get</span><span class="token punctuation">(</span>target<span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">return</span> toProxy<span class="token punctuation">.</span><span class="token function">get</span><span class="token punctuation">(</span>target<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    <span class="token keyword">if</span><span class="token punctuation">(</span>toRaw<span class="token punctuation">.</span><span class="token function">has</span><span class="token punctuation">(</span>target<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">{</span>  <span class="token comment">//&#x9632;&#x6B62;&#x591A;&#x6B21;&#x4EE3;&#x7406;</span>
        <span class="token keyword">return</span> target
    <span class="token punctuation">}</span>
    <span class="token keyword">const</span> handler <span class="token operator">=</span> <span class="token punctuation">{</span>
        <span class="token keyword">get</span><span class="token punctuation">(</span>target<span class="token punctuation">,</span> key<span class="token punctuation">,</span> receiver<span class="token punctuation">)</span> <span class="token punctuation">{</span>
            <span class="token keyword">const</span> proxyTarget <span class="token operator">=</span>  Reflect<span class="token punctuation">.</span><span class="token function">get</span><span class="token punctuation">(</span>target<span class="token punctuation">,</span> key<span class="token punctuation">,</span> receiver<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// &#x76F8;&#x5F53;&#x4E8E; return target[key]</span>
            <span class="token keyword">if</span><span class="token punctuation">(</span><span class="token function">isObject</span><span class="token punctuation">(</span>target<span class="token punctuation">[</span>key<span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
                <span class="token keyword">return</span> <span class="token function">reactive</span><span class="token punctuation">(</span>proxyTarget<span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token punctuation">}</span>
            <span class="token keyword">return</span> proxyTarget<span class="token punctuation">;</span>
        <span class="token punctuation">}</span><span class="token punctuation">,</span>
        <span class="token keyword">set</span><span class="token punctuation">(</span>target<span class="token punctuation">,</span> key<span class="token punctuation">,</span> value<span class="token punctuation">,</span> receiver<span class="token punctuation">)</span> <span class="token punctuation">{</span>
            <span class="token comment">// &#x53EA;&#x5BF9;&#x79C1;&#x6709;&#x5C5E;&#x6027;&#x7684;&#x4FEE;&#x6539;&#x52A8;&#x4F5C;&#x89E6;&#x53D1;&#x89C6;&#x56FE;&#x66F4;&#x65B0;</span>
            <span class="token keyword">if</span><span class="token punctuation">(</span><span class="token operator">!</span>target<span class="token punctuation">.</span><span class="token function">hasOwnProperty</span><span class="token punctuation">(</span>key<span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
                <span class="token function">trigger</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token punctuation">}</span>
            <span class="token keyword">return</span> Reflect<span class="token punctuation">.</span><span class="token function">set</span><span class="token punctuation">(</span>target<span class="token punctuation">,</span> key<span class="token punctuation">,</span> value<span class="token punctuation">,</span> receiver<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// &#x76F8;&#x5F53;&#x4E8E; target[key] = value</span>
        <span class="token punctuation">}</span>
    <span class="token punctuation">}</span><span class="token punctuation">;</span>

    <span class="token comment">// &#x5229;&#x7528; Proxy &#x6765;&#x4EE3;&#x7406;&#x8FD9;&#x4E2A;&#x5BF9;&#x8C61;&#x5C5E;&#x6027;</span>
    <span class="token keyword">let</span> observed <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Proxy</span><span class="token punctuation">(</span>target<span class="token punctuation">,</span> handler<span class="token punctuation">)</span><span class="token punctuation">;</span>

    toProxy<span class="token punctuation">.</span><span class="token function">set</span><span class="token punctuation">(</span>target<span class="token punctuation">,</span> observed<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// &#x4FDD;&#x5B58;&#x5DF2;&#x4EE3;&#x7406;&#x7684;&#x5BF9;&#x8C61;</span>
    toProxy<span class="token punctuation">.</span><span class="token function">set</span><span class="token punctuation">(</span>observed<span class="token punctuation">,</span> target<span class="token punctuation">)</span><span class="token punctuation">;</span> 
    <span class="token keyword">return</span> observed<span class="token punctuation">;</span>
<span class="token punctuation">}</span>

<span class="token comment">// &#x63D0;&#x793A;&#x89C6;&#x56FE;&#x9700;&#x8981;&#x66F4;&#x65B0;</span>
<span class="token keyword">function</span> <span class="token function">trigger</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token string">&apos;&#x89C6;&#x56FE;&#x9700;&#x8981;&#x66F4;&#x65B0;&apos;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>

<span class="token keyword">function</span> <span class="token function">isObject</span><span class="token punctuation">(</span><span class="token parameter">param</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">return</span> <span class="token keyword">typeof</span> param <span class="token operator">===</span> <span class="token string">&apos;object&apos;</span> <span class="token operator">&amp;&amp;</span> param <span class="token operator">!==</span> <span class="token keyword">null</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre>
<blockquote>
<p>&#x5EFA;&#x7ACB;&#x4E00;&#x4E2A;&#x9875;&#x9762;&#x6765;&#x8BD5;&#x9A8C;&#x4E00;&#x4E0B;&#x7ED3;&#x679C;</p>
</blockquote>
<pre class="language-"><code><span class="token doctype">&lt;!DOCTYPE html&gt;</span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>html</span> <span class="token attr-name">lang</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">&quot;</span>en<span class="token punctuation">&quot;</span></span><span class="token punctuation">&gt;</span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>head</span><span class="token punctuation">&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>meta</span> <span class="token attr-name">charset</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">&quot;</span>UTF-8<span class="token punctuation">&quot;</span></span><span class="token punctuation">&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>meta</span> <span class="token attr-name">name</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">&quot;</span>viewport<span class="token punctuation">&quot;</span></span> <span class="token attr-name">content</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">&quot;</span>width=device-width, initial-scale=1.0<span class="token punctuation">&quot;</span></span><span class="token punctuation">&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>meta</span> <span class="token attr-name">http-equiv</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">&quot;</span>X-UA-Compatible<span class="token punctuation">&quot;</span></span> <span class="token attr-name">content</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">&quot;</span>ie=edge<span class="token punctuation">&quot;</span></span><span class="token punctuation">&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>title</span><span class="token punctuation">&gt;</span></span>reactive<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>title</span><span class="token punctuation">&gt;</span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>head</span><span class="token punctuation">&gt;</span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>body</span><span class="token punctuation">&gt;</span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>script</span> <span class="token attr-name">src</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">&quot;</span>demo.js<span class="token punctuation">&quot;</span></span><span class="token punctuation">&gt;</span></span><span class="token script"></span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>script</span><span class="token punctuation">&gt;</span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>script</span><span class="token punctuation">&gt;</span></span><span class="token script"><span class="token language-javascript">

<span class="token comment">// var obj = {</span>
<span class="token comment">//     name: &apos;jjjs&apos;</span>
<span class="token comment">// }</span>

<span class="token comment">// var proxyObj = reactive(obj);</span>

<span class="token comment">// &#x4FEE;&#x6539; name &#x5C5E;&#x6027;&#xFF0C;&#x53D1;&#x73B0;&#x53EF;&#x4EE5;&#x76D1;&#x63A7;&#x5230;</span>
<span class="token comment">// proxyObj.name = &apos;new Name&apos;;</span>
<span class="token comment">// console.log(proxyObj.name);</span>

<span class="token comment">// &#x5BF9;&#x4E8E;&#x672C;&#x6765;&#x4E0D;&#x5B58;&#x5728;&#x7684;&#x5C5E;&#x6027;&#x4E5F;&#x53EF;&#x4EE5;&#x8FDB;&#x884C;&#x76D1;&#x63A7;</span>
<span class="token comment">// proxyObj.age = 6;</span>
<span class="token comment">// console.log(proxyObj.age);</span>


<span class="token comment">// &#x5BF9;&#x4E8E;&#x6570;&#x7EC4;&#xFF0C;&#x4F1A;&#x51FA;&#x73B0;&#x4E24;&#x6B21;&#x89E6;&#x53D1;</span>
<span class="token comment">// let array = [1,2,3];</span>
<span class="token comment">// let obArray = reactive(array);</span>
<span class="token comment">// obArray.push(4)</span>

<span class="token comment">// &#x5BF9;&#x4E8E;&#x5D4C;&#x5957;&#x7684;&#x5BF9;&#x8C61;</span>
<span class="token keyword">var</span> obj <span class="token operator">=</span> <span class="token punctuation">{</span>
    name<span class="token punctuation">:</span> <span class="token string">&apos;jjjs&apos;</span><span class="token punctuation">,</span>
    array<span class="token punctuation">:</span> <span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token number">2</span><span class="token punctuation">,</span><span class="token number">3</span><span class="token punctuation">]</span>
<span class="token punctuation">}</span>

<span class="token comment">// &#x591A;&#x6B21;&#x83B7;&#x53D6;&#x4EE3;&#x7406;&#x7ED3;&#x679C;</span>
<span class="token keyword">var</span> proxyObj <span class="token operator">=</span> <span class="token function">reactive</span><span class="token punctuation">(</span>obj<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">var</span> proxyObj <span class="token operator">=</span> <span class="token function">reactive</span><span class="token punctuation">(</span>obj<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">var</span> proxyObj <span class="token operator">=</span> <span class="token function">reactive</span><span class="token punctuation">(</span>obj<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">var</span> proxyObj <span class="token operator">=</span> <span class="token function">reactive</span><span class="token punctuation">(</span>obj<span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token comment">// // &#x4EC5;&#x64CD;&#x4F5C;&#x4E00;&#x6B21;&#x6570;&#x7EC4;</span>
<span class="token comment">// proxyObj.array.push(4);</span>


</span></span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>script</span><span class="token punctuation">&gt;</span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>body</span><span class="token punctuation">&gt;</span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>html</span><span class="token punctuation">&gt;</span></span>
</code></pre><!-- # 依赖收集

依赖收集.也就是发布订阅模式。在Vue 3中 有一个函数effect 就是在reactive函数执行后会立即执行一次effect方法.
effect 例子：
``` js
//栈
let effectStack = [];


function effect(fn){
    //需要把fn 的函数变成响应式的函数
    let effect = createEffect(fn);
    //默认执行一次
    effect();
}
function createEffect(fn){
    let effect = function(){ //创建响应式的effect
        return run(effect,fn)// 让fn 执行 把 effect 存到栈中
    }
    return effect
}

function run(effect,fn){
    effectStack.push(effect)
    fn()
}
//在proxy的get方法中收集依赖
 get(target, key, receiver) {
    const proxyTarget =  Reflect.get(target, key, receiver); // 相当于 return target[key]

    //收集依赖 把key  和effect 对应
    track(target,key)  //如果目标上这个值变化了重新让数组中的effect执行


    if(isObject(target[key])) {
        return reactive(proxyTarget);
    }
    return proxyTarget;
},

function track(target,key){
    let effect = 
}

let obj = reactive({name:'123'})
effect(()=>{ //effect 会默认执行一次 然后等到依赖的数据变化了 会再执行一次
    console.log(obj.name)
})
obj.name = '456'
``` -->
<footer class="page-footer"><span class="copyright">Copyright &#xA9; YoungGary 2019 all right reserved&#xFF0C;powered by Gitbook</span><span class="footer-modification">&#x8BE5;&#x6587;&#x4EF6;&#x4FEE;&#x8BA2;&#x65F6;&#x95F4;&#xFF1A;
2020-01-03 16:31:06
</span></footer>
                                
                                </section>
                            
    </div>
    <div class="search-results">
        <div class="has-results">
            
            <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
            <ul class="search-results-list"></ul>
            
        </div>
        <div class="no-results">
            
            <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
            
        </div>
    </div>
</div>

                        </div>
                    </div>
                
            </div>

            
                
                <a href="./" class="navigation navigation-prev " aria-label="Previous page: JavaScript进阶">
                    <i class="fa fa-angle-left"></i>
                </a>
                
                
                <a href="el.html" class="navigation navigation-next " aria-label="Next page: 2.Element UI 源码初探">
                    <i class="fa fa-angle-right"></i>
                </a>
                
            
        
    </div>

    <script>
        var gitbook = gitbook || [];
        gitbook.push(function() {
            gitbook.page.hasChanged({"page":{"title":"1.Vue.js 3.0中的响应式","level":"1.5.1","depth":2,"next":{"title":"2.Element UI 源码初探","level":"1.5.2","depth":2,"path":"js/el.md","ref":"js/el.md","articles":[]},"previous":{"title":"JavaScript进阶","level":"1.5","depth":1,"path":"js/README.md","ref":"js/README.md","articles":[{"title":"1.Vue.js 3.0中的响应式","level":"1.5.1","depth":2,"path":"js/proxy.md","ref":"js/proxy.md","articles":[]},{"title":"2.Element UI 源码初探","level":"1.5.2","depth":2,"path":"js/el.md","ref":"js/el.md","articles":[]},{"title":"3.bind的模拟实现","level":"1.5.3","depth":2,"path":"js/bind.md","ref":"js/bind.md","articles":[]},{"title":"4.JS原型和原型链","level":"1.5.4","depth":2,"path":"js/proto.md","ref":"js/proto.md","articles":[]},{"title":"5.模拟实现call和apply","level":"1.5.5","depth":2,"path":"js/call.md","ref":"js/call.md","articles":[]},{"title":"6.使用Vue cli3搭建组件库并发布到 npm","level":"1.5.6","depth":2,"path":"js/ui.md","ref":"js/ui.md","articles":[]},{"title":"7.记一次基于qiankun的微前端的改造","level":"1.5.7","depth":2,"path":"js/microFE.md","ref":"js/microFE.md","articles":[]},{"title":"8.Git的使用","level":"1.5.8","depth":2,"path":"js/git.md","ref":"js/git.md","articles":[]}]},"dir":"ltr"},"config":{"plugins":["theme-comscore","prism","-highlight","copy-code-button","search-pro","-search","-lunr","expandable-chapters","splitter","-sharing","tbfed-pagefooter","anchor-navigation-ex"],"styles":{},"pluginsConfig":{"tbfed-pagefooter":{"copyright":"Copyright &copy YoungGary 2019","modify_label":"该文件修订时间：","modify_format":"YYYY-MM-DD HH:mm:ss"},"prism":{"css":["prismjs/themes/prism-solarizedlight.css"],"lang":{"shell":"bash"}},"splitter":{},"search-pro":{},"fontsettings":{"theme":"white","family":"sans","size":2},"anchor-navigation-ex":{"associatedWithSummary":true,"float":{"floatIcon":"fa fa-navicon","level1Icon":"","level2Icon":"","level3Icon":"","showLevelIcon":false},"mode":"float","multipleH1":true,"pageTop":{"level1Icon":"","level2Icon":"","level3Icon":"","showLevelIcon":false},"printLog":false,"showGoTop":true,"showLevel":false},"theme-comscore":{},"copy-code-button":{},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false},"expandable-chapters":{}},"theme":"default","author":"Gary","pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"variables":{},"title":"Gary的小窝","language":"zh-hans","gitbook":"*","description":"Gary的小窝. 里面包含了个人撰写的技术文章"},"file":{"path":"js/proxy.md","mtime":"2020-01-03T08:31:06.073Z","type":"markdown"},"gitbook":{"version":"3.2.3","time":"2021-09-09T14:10:10.930Z"},"basePath":"..","book":{"language":""}});
        });
    </script>
</div>

        
    <script src="../gitbook/gitbook.js"></script>
    <script src="../gitbook/theme.js"></script>
    
        
        <script src="../gitbook/gitbook-plugin-copy-code-button/toggle.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-search-pro/jquery.mark.min.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-search-pro/search.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-expandable-chapters/expandable-chapters.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-splitter/splitter.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-theme-comscore/test.js"></script>
        
    

    </body>
</html>

